package jianzhi;

import niuke.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * description:
 * author:zt
 * date:2021-10-21
 */
//从上到下打印出二叉树的每个节点，同一层的节点按照从左到右的顺序打印。
public class jianzhi32 {
    public int[] levelOrder(TreeNode root) {
        if (root == null) return new int[]{};
        ArrayList<Integer> res = new ArrayList<>();
        LinkedList<TreeNode> list = new LinkedList<>();
        list.addLast(root);
        while (!list.isEmpty()){
            TreeNode node = list.removeFirst();
            if (node.left!=null) list.addLast(node.left);
            if (node.right!=null) list.addLast(node.right);
            res.add(node.val);
        }
        int[] ans = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            ans[i] = res.get(i);
        }
        return ans;
    }
}
